package com.rf.richfitwheel.workflow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.WfAuthRuleFieldMapper;
import com.rf.richfitwheel.workflow.model.WfAuthRuleField;
import com.rf.richfitwheel.workflow.model.WfAuthRuleFieldExample;
import com.rf.richfitwheel.workflow.service.WfAuthRuleFieldService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Service("wfAuthRuleFieldService")
public class WfAuthRuleFieldServiceImpl implements WfAuthRuleFieldService {

    @Autowired
    private WfAuthRuleFieldMapper wfAuthRuleFieldMapper;

    @Override
    public void updateWfAuthRuleField(WfAuthRuleField wfAuthRuleField) {
        wfAuthRuleFieldMapper.updateByPrimaryKeySelective(wfAuthRuleField);
    }

    @Override
    public void addWfAuthRuleField(WfAuthRuleField wfAuthRuleField) {
        wfAuthRuleField.setId(UuidUtil.get32UUID());
        wfAuthRuleFieldMapper.insert(wfAuthRuleField);
    }

    @Override
    public WfAuthRuleField getById(String id) {
        return wfAuthRuleFieldMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        for (String id : ids) {
            wfAuthRuleFieldMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void deleteById(String id) {
        wfAuthRuleFieldMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Map<String, Object>> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        WfAuthRuleField wfAuthRuleField = (WfAuthRuleField) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), WfAuthRuleField.class);
        WfAuthRuleFieldExample example = new WfAuthRuleFieldExample();
        WfAuthRuleFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(wfAuthRuleField.getFolderCode());
        List<WfAuthRuleField> list = wfAuthRuleFieldMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public List<WfAuthRuleField> getListByFolderCode(String folderCode) {
        WfAuthRuleFieldExample example = new WfAuthRuleFieldExample();
        WfAuthRuleFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(folderCode);
        List<WfAuthRuleField> list = wfAuthRuleFieldMapper.selectByExample(example);
        return list;
    }

    @Override
    public WfAuthRuleField findByFolderCodeAndRuleField(String folderCode, String ruleField) {
        WfAuthRuleFieldExample example = new WfAuthRuleFieldExample();
        WfAuthRuleFieldExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(folderCode);
        criteria.andRuleFieldEqualTo(ruleField);
        List<WfAuthRuleField> list = wfAuthRuleFieldMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean checkRuleField(String folderCode, String ruleField) {
        WfAuthRuleField wfAuthRuleField = this.findByFolderCodeAndRuleField(folderCode, ruleField);
        if (ObjectUtils.isEmpty(wfAuthRuleField)) {
            return false;
        }
        return true;
    }
}
